﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.GovernmentOfficeRegion
{
    public partial class GovOfficeRegionDetails : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadGovOfficeRegion();
            }
        }

        /// <summary>
        /// Loads the gov office region.
        /// </summary>
        private void LoadGovOfficeRegion()
        {
            int id = 0;
            if (int.TryParse(Request.Params[0], out id))
            {
                GovOfficeRegion govOfficeRegion = GovOfficeRegionBO.GetGovOfficeRegionById(id);
                if (govOfficeRegion != null)
                {
                    GovOfficeRegionFormView.DataSource = new[] {govOfficeRegion};
                    GovOfficeRegionFormView.DataBind();

                    var textBoxCounty = GovOfficeRegionFormView.FindControl("TextBoxCounty") as TextBox;
                    if (textBoxCounty != null)
                    {
                        textBoxCounty.Text = string.Join("\r\n", govOfficeRegion.Counties.Select(c => c.CountyName));
                    }
                    return;
                }
            }

            GovOfficeRegionFormView.DataSource = new object[0];
            GovOfficeRegionFormView.DataBind();
        }
    }
}